var limsLocation = Gikam.getPageObject({

    getSelectInfoFormFields: function () {
        return [{
            field: 'storage',
            title : 'T_LIMS_LOCATION.STORAGE',
            type : 'select',
            category : 'sys',
            param : {
                codeCategoryId_EQ : 'storage'
            },
            titleAlign: 'left',
            onChange : function (field, value, index) {
                let parent = 1;
                let requestGridParam = {};
                requestGridParam.parentId_EQ = 1;
                requestGridParam.createdByOrgId_EQ = workspace.user.ext$.loginorgid;
                let requestTreeParam = {};
                requestTreeParam.createdByOrgId_EQ = workspace.user.ext$.loginorgid;

                if (Gikam.isEmpty(value)){
                    requestGridParam.parentId_EQ = 1;
                    requestGridParam.createdByOrgId_EQ = workspace.user.ext$.loginorgid;
                    requestTreeParam.createdByOrgId_EQ = workspace.user.ext$.loginorgid;

                    requestGridParam.storage_EQ = null;
                    requestTreeParam.storage_EQ = null;
                }

                if (Gikam.isNotEmpty(value)){
                    requestGridParam.storage_EQ = value;
                    requestTreeParam.storage_EQ = value;
                }

                if (Gikam.isNotEmpty(this.options.data.locationDesc)){
                    requestGridParam.parentId_EQ = this.options.data.locationDesc;
                    parent = this.options.data.locationDesc;
                }

                let tab = Gikam.getComp('location-tab');
                tab.revertPanel('location-panel');
                tab.removePanel('board-panel');
                Gikam.getAsyncComp('location-edit-list-grid').done(function(grid) {
                    grid.refresh({
                        url: limsLocation.baseUrl + '/queries',
                        requestData : requestGridParam
                    });
                });
                Gikam.getAsyncComp('lims-location-tree-grid').done(function (grid) {
                    grid.refresh({
                        url : limsLocation.baseUrl + '/tree/' + parent,
                        requestData : requestTreeParam
                    });
                });
                Gikam.getComp('sample-Location').setData([]);
            }
        },{
            field: 'locationDesc',
            title: 'T_LOCATION.BX',
            type: 'select',
            search: true,
            category: 'refrigeratorChoose',
            titleAlign: 'left',
            firstBlank: true,
            param: {
                parentId_EQ: 1,
            },
            onChange: function (field, value, ss) {
                var parent = 1;
                if(Gikam.isNotEmpty(value)){
                    parent = value
                }
                let tab = Gikam.getComp('location-tab');
                tab.revertPanel('location-panel');
                tab.removePanel('board-panel');
                Gikam.getAsyncComp('location-edit-list-grid').done(function(grid) {
                    grid.refresh({
                        url: limsLocation.baseUrl + '/queries',
                        requestData : {
                            parentId_EQ : parent,
                            createdByOrgId_EQ: workspace.user.ext$.loginorgid,
                        }
                    });
                });
                Gikam.getAsyncComp('lims-location-tree-grid').done(function (grid) {
                    grid.refresh({
                        url : limsLocation.baseUrl + '/tree/' + parent,
                        createdByOrgId_EQ: workspace.user.ext$.loginorgid,
                    });
                });
                Gikam.getComp('sample-Location').setData([]);
            },
        }];
    },
    
    getEditListColumns : function() {
        return [ {
            checkbox : true
        }, {index: true},{
            field : 'locationNo',
            title : 'T_LIMS_LOCATION.LOCATIONNO'
        }, {
            field : 'locationDesc',
            title : 'T_LIMS_LOCATION.LOCATIONDESC',
            editor : true
        }, {
            field : 'level',
            title : 'T_LIMS_LOCATION.LEVEL'
        } ];
    },

    getBoardListColumns : function() {
        return [ {
            checkbox : true
        }, {index: true},{
            field : 'boardCode',
            title : 'T_LIMS_LOCATION_BOARD.BOARDCODE'
        }, {
            field : 'boardName',
            title : 'T_LIMS_LOCATION_BOARD.BOARDNAME'
        }, {
            field : 'ext$.locationdesc',
            title : 'T_LIMS_LOCATION.LOCATIONDESC'
        }, {
            field : 'boardType',
            title : 'T_LIMS_LOCATION_BOARD.BOARDTYPE',
            type : 'select',
            category : 'sys',
            firstBlank : false,
            param : {
                codeCategoryId : 'boardType'
            },
        }, {
            field : 'rowNum',
            title : 'T_LIMS_LOCATION_BOARD.ROWNUM',
        }, {
            field : 'columnNum',
            title : 'T_LIMS_LOCATION_BOARD.COLUMNNUM',
        } ];
    },

    getHoleDetailInfoFormFields : function () {
        return [{
            field : 'sampleCode',
            title : 'T_LIMS_SAMPLE.SAMPLECODE',
        }, {
            field : 'sampleType',
            title : 'T_LIMS_SAMPLE.SAMPLETYPE',
        }, {
            field : 'sampleName',
            title : 'T_LIMS_SAMPLE.SAMPLENAME',
        }];
    },

    getBaseInfoFormFields : function(store) {
        return [ {
            field : 'storage',
            title : 'T_LIMS_LOCATION.STORAGE',
            validators:['notEmpty'],
            value : store,
            preInsert : true,
            type : 'select',
            category : 'sys',
            firstBlank : false,
            param : {
                codeCategoryId: 'storage'
            }
        },{
            field : 'storageType',
            title : 'T_LIMS_LOCATION.LOCATIONTYPE',
            validators: ['notEmpty'],
            preInsert : true,
            type : 'select',
            category : 'sys',
            firstBlank : false,
            param : {
                codeCategoryId : 'storageType'
            }
        }, {
            field : 'storageCode',
            title : 'T_LOCATION.BXNAME',
            validators: ['notEmpty'],
            preInsert : true
        }, {
            field : 'ext$.cs',
            validators : ['integer', 'numRange(0, 20]'],
            title : 'T_LOCATION.CS',
            preInsert : true
        }, {
            field : 'ext$.jzs',
            validators : ['integer', 'numRange(0, 20]'],
            title : 'T_LOCATION.JZS',
            preInsert : true
        }, {
            field : 'ext$.hzs',
            validators : ['integer', 'numRange(0, 20]'],
            title : 'T_LOCATION.HZS',
            preInsert : true
        }];
    },

    getChooseListColumns : function() {
        return [ {index: true},{
            field : 'locationNo',
            title : 'T_LIMS_LOCATION.LOCATIONNO'
        }, {
            field : 'locationDesc',
            title : 'T_LIMS_LOCATION.LOCATIONDESC'
        }, {
            field : 'level',
            title : 'T_LIMS_LOCATION.LEVEL'
        }, {
            field : 'group',
            title : 'T_LIMS_LOCATION.GROUP'
        }, {
            field : 'status',
            title : 'T_LIMS_LOCATION.STATUS'
        }, ];
    },

    getAddInfoFormFields : function(node, selectedNode) {
        let arr = [node.locationDesc];
        let level = parseInt(selectedNode.level);
        if (node.childQty > 0 && !Gikam.isEmpty(node.children)) {
            limsLocation.getLocationDesc(node.children, arr, selectedNode);
        }

        return [ {
            field : 'storage',
            title : 'T_LIMS_LOCATION.STORAGE',
            validators:['notEmpty'],
            preInsert : true,
            type : 'select',
            category : 'sys',
            firstBlank : false,
            value: selectedNode.storage,
            readonly : true,
            param : {
                codeCategoryId: 'storage'
            }
        },{
            field : 'storageType',
            title : 'T_LIMS_LOCATION.LOCATIONTYPE',
            validators: ['notEmpty'],
            preInsert : true,
            type : 'select',
            category : 'sys',
            firstBlank : false,
            param : {
                codeCategoryId : 'storageType'
            },
            value: selectedNode.storageType,
            readonly: true
        }, {
            field : 'ext$.bx',
            title : 'T_LOCATION.BXNAME',
            validators: ['notEmpty'],
            preInsert : true,
            value: level > 0?arr[0]:null,
            readonly: level > 0
        }, {
            field : 'ext$.cs',
            validators : level > 1?[]:['integer', 'numRange(0, 20]'],
            title : level > 1?'T_LOCATION.CMC':'T_LOCATION.CS',
            preInsert : true,
            value: level > 1?arr[1]:null,
            readonly: level > 1
        }, {
            field : 'ext$.jzs',
            validators : level > 2?[]:['integer', 'numRange(0, 20]'],
            title : level > 2?'T_LOCATION.JZMC':'T_LOCATION.JZS',
            preInsert : true,
            value: level > 2?arr[2]:null,
            readonly: level > 2
        }, {
            field : 'ext$.hzs',
            validators : level > 3?[]:['integer', 'numRange(0, 20]'],
            title : level > 3?'T_LOCATION.HZMC':'T_LOCATION.HZS',
            preInsert : true,
            value: level > 3?arr[3]:null,
            readonly: level > 3
        }];
    },

    // 递归获取当前节点位置名称
    getLocationDesc: function(treeList, arr, selectedNode) {
        for (let i = 0; i < treeList.length; i++) {
            const node = treeList[i];
            let level = parseInt(node.level);
            arr[level-1] = node.locationDesc;
            if (node.id === selectedNode.id ){
                return true;
            }
            if (node.childQty > 0 && !Gikam.isEmpty(node.children)) {
                if (limsLocation.getLocationDesc(node.children, arr, selectedNode)) {
                    return true;
                }
            }
        }
    },

    // 递归获取当前节点
    getCurrentNode: function(treeList, selectedNode, level) {
        for (let i = 0; i < treeList.length; i++) {
            const node = treeList[i];
            if (node.id === selectedNode.id ){
                return node;
            }
            if (node.childQty > 0 && !Gikam.isEmpty(node.children)) {
                const targetNode = limsLocation.getCurrentNode(node.children, selectedNode);
                if (!Gikam.isEmpty(targetNode)) {
                    return targetNode;
                }
            }
        }
    },

    // 递归获取当前节点下的所有子节点id
    getSelectionsByNode: function(treeList, selections) {
        for (let i = 0; i < treeList.length; i++) {
            const node = treeList[i];
            if (!Gikam.isEmpty(node) && !Gikam.isEmpty(node.id)) {
                selections.push({
                    id : node.id
                });
            }
            if (node.childQty > 0 && !Gikam.isEmpty(node.children)) {
                limsLocation.getSelectionsByNode(node.children, selections);
            }
        }
    },

    baseUrl : IFM_CONTEXT + '/secure/baseline/samplemanage/locations',
    boardUrl : IFM_CONTEXT + '/secure/baseline/samplemanage/location-boards',
    holeUrl : IFM_CONTEXT + '/secure/baseline/samplemanage/location-board-holes',
    sampleUrl : IFM_CONTEXT + '/secure/baseline/samplemanage/samples'
});
